package com.cry.todo.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cry.core.security.UserDetailsService;
import com.cry.todo.dao.ProjectDao;
import com.cry.todo.domain.User;
import com.cry.todo.domain.audit.AuditRevision;
import com.cry.todo.domain.project.Category;
import com.cry.todo.domain.project.Project;
import com.cry.todo.domain.project.ProjectSearch;
import com.cry.todo.domain.project.Status;
import com.cry.todo.service.ProjectService;

@Repository
@Transactional(propagation=Propagation.REQUIRED)
public class ProjectServiceImpl implements ProjectService {
	@Autowired
	private ProjectDao projectDao;
	
	@Autowired
	UserDetailsService userDetailsService;
	
	@Override
	public Project loadById(int id) {
		return projectDao.loadById(id);
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	@Override
	public Serializable saveOrUpdate(Project item) {
		Date now = new Date(Calendar.getInstance().getTimeInMillis());
		/**
		 * Set creator if it was create, modified at if it was update
		 */
		User user = userDetailsService.getSessionUser();
		
		return projectDao.saveOrUpdate(item, user);
	}

	@Override
	public List<Project> findChildren(int parentID) {
		return projectDao.findChildren(parentID);
	}

	@Override
	public int countChildren(int id) {
		return projectDao.countChildren(id);
	}

	@Override
	public List<Project> findChildren(int id, Integer startRow, Integer displayRow) {
		return projectDao.findChildren(id, startRow, displayRow);
	}

	@Override
	public List<Project> findChildren(int id, Integer startRow, Integer displayRow, 
			String orderColumn,	String orderDir) {
		return projectDao.findChildren(id, startRow, displayRow, orderColumn, orderDir);
	}
	
	
	@Override
	public ArrayList<Project> getParents(Project item) {
		return projectDao.getParents(item);
	}

	@Override
	public ArrayList<Category> loadCategories() {
		return projectDao.loadCategories();
	}

	@Override
	public ArrayList<Status> loadStatuses() {
		return projectDao.loadStatuses();
	}

	@Override
	public Category loadCategory(Integer id) {
		return projectDao.loadCategory(id);
	}

	@Override
	public List<AuditRevision<Project>> findRevisionsbyId(int id) {
		return projectDao.findRevisionsById(id);
	}

	@Override
	public void addComment(Project item, String comment) {
		User user = userDetailsService.getSessionUser();

		projectDao.addComment(item, comment, user);
	}

	@Override
	public List<Project> search(ProjectSearch criteria) {
		return projectDao.search(criteria);
	}

	@Override
	public ProjectSearch getLasProjectSearch() {
		User user = userDetailsService.getSessionUser();
		return projectDao.getLasProjectSearch(user);
	}

	@Override
	public void saveLastProjectSearch(ProjectSearch item) {
		User user = userDetailsService.getSessionUser();
		projectDao.saveLastProjectSearch(item, user);
	}

	@Override
	public List<Project> search(ProjectSearch item, Integer startRow, Integer displayRow, String orderColumn, String orderDir) {
		return projectDao.search(item, startRow, displayRow, orderColumn, orderDir);
	}

}
